#include "sendtestpage.h"
#include <stdlib.h>
sendtestpage::sendtestpage()
{
     net_frame_send = new QTextEdit();
     net_btn_send = new QPushButton("发送");
     net_main_layout = new QGridLayout() ;
     net_frame_send_layout = new QHBoxLayout();
     new_frame_show_layout = new QVBoxLayout();
     net_frame_send_layout->addWidget(net_frame_send);
    net_frame_send_layout->addWidget(net_btn_send);
     new_frame_show_layout ->addLayout(net_frame_send_layout);
     net_main_layout->addLayout(new_frame_show_layout,0,1);
    connect(net_btn_send,SIGNAL(clicked()),this,SLOT(serial_send()));

     setLayout(net_main_layout);   //展示界面。
     resize(1000,500);
     setWindowTitle("报文发送测试");
     this->setAttribute(Qt::WA_QuitOnClose, false);
}
void sendtestpage::serial_send()
{
     QString str = net_frame_send->toPlainText();
     str.remove(QRegExp("\\s"));
     qDebug()<<"发送的数据是"<<str;
     if(str.length()%2)
     {
         net_frame_send->append("报文长度不是2的整数倍");
         return;
     }
     unsigned char buff[4096]={0};
     //字符串转换为hex
     int num;
     for(int i = 0;i < str.size();i += 2)
     {
         num = str.mid(i,2).toUInt(nullptr,16);
         buff[i/2] = num&0xFF;
    }
     memcpy(serial_channel.response_frame,buff,str.size()/2);
     serial_channel.response_len = str.size()/2;
     emit sendframeSignal();
}
